package com.sum.tea.ui

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.view.View
import com.sum.tea.base.BaseMvvmActivity
import com.sum.tea.databinding.ActivityLoadingBinding
import com.sum.tea.viewmodel.LoadingViewModel

class LoadingActivity : BaseMvvmActivity<ActivityLoadingBinding, LoadingViewModel>() {

    companion object {
        private const val TAG = "LoadingActivity"
        private const val DELAY_TIME = 50L // 延迟时间，单位：毫秒
    }

    override fun initView(savedInstanceState: Bundle?) {
        initActionBar()
        createObserver()
        startMainActivityWithDelay()
    }

    private fun initActionBar() {
        supportActionBar?.let { actionBar ->
            val spannableString = SpannableString("UHF_DEMO").apply {
                setSpan(
                    AbsoluteSizeSpan(15, true),
                    0,
                    length,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE
                )
            }
            actionBar.title = spannableString
        }
    }

    private fun createObserver() {
        mBinding.loadingBar.visibility = View.VISIBLE
        mBinding.loadingText.visibility = View.VISIBLE
    }

    private fun startMainActivityWithDelay() {
        Handler(Looper.getMainLooper()).postDelayed({
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }, DELAY_TIME)
    }
}
